
#!/bin/bash 
# Backup Script for va_stratlab DB
# Author: Andreas Voellenklee

# Setting path variables
BACKUP_DIR=../DB_BU
DB_USER=va
DB_PASS=dummy
DATUM=`date +%Y%m%d%H%M` 

# some functions
function echo_usage(){
  echo "usage   : $0 <dbname> ALL"
  echo "        : $0 <dbname> GROUPS"
  echo "        : $0 <dbname> STRUCTURE"
  echo "        : $0 <dbname> ORDERLOGIC"
}

# echo usage if no parameter given
if [ $# -lt 2 ]
then
  echo_usage
  exit;
fi

# echo usage if too many parameters given
if [ $# -gt 2 ]
then
  echo_usage
  exit;
fi

if [ $2 = "ALL" ]
then
  echo "  creating full backup of $1 into $BACKUP_DIR"
  mysqldump $1 -u $DB_USER --password=$DB_PASS > ${BACKUP_DIR}/${DATUM}_$1_full_backup.sql
  cd $BACKUP_DIR
  tar -cvzf ${DATUM}_$1_full_backup.tgz ${DATUM}_$1_full_backup.sql
  rm ${DATUM}_$1_full_backup.sql
  echo " done!"
  exit;
elif [ $2 = "GROUPS" ]
then
  echo "  creating backup of groups tables in $DB_NAME into $BACKUP_DIR"
  mysqldump $1 groupname groupmap -u $DB_USER --password=$DB_PASS > ${BACKUP_DIR}/${DATUM}_$1_groups_backup.sql
  cp ${BACKUP_DIR}/${DATUM}_$1_groups_backup.sql ${BACKUP_DIR}/$1_groups.sql 
  echo " done!"
  exit;
elif [ $2 = "STRUCTURE" ]
then
  echo "  creating backup of structure of $DB_NAME into $BACKUP_DIR"
  mysqldump -d $1 -u $DB_USER --password=$DB_PASS > ${BACKUP_DIR}/${DATUM}_$1_structure.sql
  cp ${BACKUP_DIR}/${DATUM}_$1_structure.sql ${BACKUP_DIR}/$1_structure.sql 
  echo " done!"
  exit;
elif [ $2 = "ORDERLOGIC" ]
then
  echo "  creating backup of $DB_NAME into $BACKUP_DIR"
  mysqldump $1 orderLogicTest -u $DB_USER --password=$DB_PASS > ${BACKUP_DIR}/${DATUM}_$1_orderlogic.sql
  cp ${BACKUP_DIR}/${DATUM}_$1_orderlogic.sql ${BACKUP_DIR}/$1_orderlogic.sql 
  echo " done!"
  exit;
fi

exit;

# compress data
 
#$TAR -cvzf $AKT_DATUM.backup.sql.tgz $AKT_DATUM.backup.sql 

# clean up
#$RM $AKT_DATUM.backup.sql 
